package com.wechat.controller;

import com.wechat.annotation.GlobalInterceptor;
import com.wechat.entity.config.AppConfig;
import com.wechat.entity.constants.Constants;
import com.wechat.entity.dto.SysSettingDto;
import com.wechat.entity.vo.ResponseVO;
import com.wechat.redis.RedisComponent;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/*
 * @description:设置系统配置api
 */
@RestController("adminSettingController")
@RequestMapping("/admin")
public class AdminSettingController extends ABaseController{
    @Resource
    private RedisComponent redisComponent;
    @Resource
    private AppConfig appConfig;

    /**
     * 获取系统配置
     * @return
     */
    @GetMapping("/getSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO getSysSetting() {
        SysSettingDto sysSettingDto = redisComponent.getSysSetting();
        return getSuccessResponseVO(sysSettingDto);
    }

    /**
     * 保存系统配置
     * @param sysSettingDto
     */
    @PostMapping("/saveSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO saveSysSetting(SysSettingDto sysSettingDto,
                               MultipartFile robotFile,
                               MultipartFile robotCover) throws IOException {
        if(robotFile != null){
            String baseFolder = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE;
            File targetFileFolder = new File(baseFolder + Constants.FILE_FOLDER_AVATAR_NAME);//头像目录
            if (!targetFileFolder.exists()) {
                targetFileFolder.mkdirs();
            }
            String filePath = targetFileFolder.getPath() + "/" + Constants.ROBOT_UID+ Constants.IMAGE_SUFFIX;
            robotFile.transferTo(new File(filePath));
            robotCover.transferTo(new File(filePath+Constants.COVER_IMAGE_SUFFIX));

        }
        redisComponent.saveSysSetting(sysSettingDto);
        return getSuccessResponseVO(null);
    }


}
